package com.xzmzhp.dao.finance.SQL;

import com.xzmzhp.pojo.finance.FinanceStatistics;
import com.xzmzhp.utils.DateUtil;
import com.xzmzhp.utils.StringUtils;
import org.apache.ibatis.jdbc.SQL;

import java.util.Calendar;

/**
 * @author: 王源(xm)
 * @create: 2021-11-06 16:32
 * @program: ams
 * @description: 财务统计信息sql
 */
public class FinanceStatisticsSql {

    /**
     * 根据时间查询财务统计信息
     *
     * @param startDate 开始时间
     * @param endDate   结束时间
     * @return 财务统计信息
     */
    public String findFinanceStatisticsByDate(String startDate, String endDate) {
        return new SQL() {
            {
                SELECT("fstatistics_id,balance,expenses_total,income_total,creation_time");
                FROM("finance_statistics");
                if (StringUtils.isNotNull(startDate)) {
                    WHERE("creation_time >=" + startDate);
                }
                if (StringUtils.isNotNull(endDate)) {
                    WHERE("creation_time <=" + endDate);
                }
            }
        }.toString();
    }

    /**
     * 根据时间类型查询财务信息
     * @param dateType 时间类型
     * @return 月、年财务信息
     */
    public String findFinanceStatisticsByDateType(Integer dateType){
        return new SQL() {
            {
                if (dateType.compareTo(Calendar.MONTH)==0){
                    SELECT("sun(balance) as balance,sun(expenses_total) as expenses_total ,sun(income_total) as income_total ,DATE_FORMAT(creation_time,'%Y-%m') as creation_time");
                }else if (dateType.compareTo(Calendar.YEAR)==0){
                    SELECT("sun(balance) as balance,sun(expenses_total) as expenses_total ,sun(income_total) as income_total ,DATE_FORMAT(creation_time,'%Y') as creation_time");
                } else{
                    SELECT("sun(balance) as balance,sun(expenses_total) as expenses_total ,sun(income_total) as income_total ,creation_time");
                }
                FROM("finance_statistics");
                if (dateType.compareTo(Calendar.MONTH)==0){
                    WHERE("DATE_FORMAT(creation_time,'%Y') = "+ DateUtil.getNowYear());
                }else if (dateType.compareTo(Calendar.YEAR)==0){
                    WHERE("1 = 1");
                }else {
                    WHERE("DATE_FORMAT(creation_time,'%Y-%m') = "+ DateUtil.getBeginDayOfMonth());
                }
                GROUP_BY("creation_time");
                ORDER_BY("creation_time");
            }
        }.toString();
    }

    /**
     * 修改财务统计信息
     * @param financeStatistics 财务信息
     * @return 修改sql语句
     */
    public String editFinanceStatistics(FinanceStatistics financeStatistics){
        return new SQL(){
            {
                UPDATE("finance_statistics");
                SET("balance = '" + financeStatistics.getBalance() + "'");
                //判断总计支出是否有值
                if (StringUtils.isNotNull(financeStatistics.getExpensesTotal())) {
                    //修改总计支出字段
                    SET("expenses_total = '" + financeStatistics.getExpensesTotal() + "'");
                }
                //判断总计收入字段是否有值
                if (StringUtils.isNotNull(financeStatistics.getIncomeTotal())) {
                    //修改总计收入字段
                    SET("income_total = '" + financeStatistics.getIncomeTotal() + "'");
                }
                //判断财务统计id字段是否有值
                if (StringUtils.isNotNull(financeStatistics.getFstatisticsId())) {
                    //财务统计id字段
                    WHERE("fstatistics_id = " + financeStatistics.getFstatisticsId());
                } else {
                    WHERE("fstatistics_id = " + 0);
                }
            }
        }.toString();
    }
}
